package com.cares.viewpagergallery;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;

public class ViewFlipperGallery extends AppCompatActivity implements View.OnTouchListener {

    private ViewFlipper mFlipper;
    private GestureDetector mDetector; //手势检测
    private TextView tv_num;
    private int num = 1;
    private int total = 3;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFlipper = findViewById(R.id.vp_content);
        tv_num = findViewById(R.id.tv_num);


        mFlipper.addView(getImageView("https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3175508956,2902264390&fm=26&gp=0.jpg"));
        mFlipper.addView(getImageView("https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3858017296,3753519258&fm=26&gp=0.jpg"));
        mFlipper.addView(getImageView("https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2933900077,2157090729&fm=26&gp=0.jpg"));
        mFlipper.setOnTouchListener(this);
        tv_num.setText(num + "/" + total);

        mDetector = new GestureDetector(new simpleGestureListener());
    }

    private ImageView getImageView(String url) {
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        GlideUtils.showImg(this, url, imageView);
        return imageView;
    }

    public boolean onTouch(View v, MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener {
        final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;

        //不知道为什么，不加上onDown函数的话，onFling就不会响应，真是奇怪
        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            // Fling left
            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

                mFlipper.showNext();
                if (num == total) {
                    num = 1;
                } else {
                    num++;
                }
                tv_num.setText(num + "/" + total);
            } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                    && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                // Fling right

                mFlipper.showPrevious();
                if (num == 1) {
                    num = total;
                } else {
                    num--;
                }
                tv_num.setText(num + "/" + total);
            }
            return true;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }
}